package com.smartfast4j.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartfast4j.demo.bean.ResponseResult;
import com.smartfast4j.demo.pojo.LearnResource;
import com.smartfast4j.demo.pojo.LearnResourceExample;
import com.smartfast4j.demo.service.LearnResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * restful api来展示
 * Created by gacl on 2017/11/17.
 */
@RestController
@RequestMapping("/learnResource")
public class LearnResourceController {

    @Autowired
    private LearnResourceService learnResourceService;

    /**
     *
     * @param page 当前页数,注意这是jqgrid自身的参数
     * @param rows 每页显示行数，,注意这是jqgrid自身的参数
     * @param author
     * @param title
     * @return
     */
   @RequestMapping("/list")
    public ResponseResult getLearnResouceList(Integer page,Integer rows,String author,String title) {
       if (page != null && rows != null) {
           //分页查询
           PageHelper.startPage(page, rows);
       }
       LearnResourceExample example = new LearnResourceExample();
       LearnResourceExample.Criteria criteria = example.createCriteria();
       if (StringUtils.isNotEmpty(author)) {
           criteria.andAuthorLike("%" + author + "%");
       }
       if (StringUtils.isNotEmpty(title)) {
           criteria.andTitleLike("%" + title + "%");
       }
       //只有紧跟在 PageHelper.startPage 方法后的第一个 MyBatis 的查询(select)方法会被分页。
       List<LearnResource> learnList= learnResourceService.selectByExample(example);
       PageInfo<LearnResource> pageInfo =new PageInfo<>(learnList);

       return ResponseResult
               .ok()
               .put("rows", learnList)
               .put("total", pageInfo.getPages())
               .put("records", pageInfo.getTotal());
   }

    @GetMapping("/{id}")
    public  LearnResource getLearnResouceById(@PathVariable("id") Long id){
        return learnResourceService.selectByPrimaryKey(id);
    }

    @RequestMapping("/add")
    public ResponseResult add(LearnResource learnResouce){
        if(StringUtils.isBlank(learnResouce.getAuthor())){
            return ResponseResult.error(-1,"作者不能为空!");
        }
        if(StringUtils.isBlank(learnResouce.getTitle())){
            return ResponseResult.error(-1,"教程名称不能为空!");
        }
        if(StringUtils.isBlank(learnResouce.getUrl())){
            return ResponseResult.error(-1,"教程链接地址不能为空!");
        }
        int result = learnResourceService.insertSelective(learnResouce);
        if(result>0){
            return ResponseResult.ok("教程添加成功!");
        }else{
            return ResponseResult.error(-1,"教程添加失败!");
        }
    }

    @RequestMapping("/update")
    public ResponseResult update(LearnResource learnResouce){
        int result = learnResourceService.updateByPrimaryKeySelective(learnResouce);
        if(result>0){
            return ResponseResult.ok("教程修改成功!");
        }else{
            return ResponseResult.error(-1,"教程修改失败!");
        }
    }

    @RequestMapping("/delete")
    public ResponseResult delete(String ids){
        List<Long> idList = new ArrayList<>();
        for(String id:ids.split(",")){
            idList.add(Long.parseLong(id));
        }
        LearnResourceExample example = new LearnResourceExample();
        example.createCriteria().andIdIn(idList);
        int result = learnResourceService.deleteByExample(example);
        if(result>0){
            return ResponseResult.ok("教程删除成功!");
        }else{
            return ResponseResult.error(-1,"教程删除失败!");
        }
    }
}
